/**
 * %SVN.HEADER%
 */
package junit.featureselection;

import java.io.File;
import java.io.IOException;

import net.sf.javaml.core.Dataset;
import net.sf.javaml.tools.data.FileHandler;
import net.sf.javaml.tools.weka.WekaAttributeSelection;

import org.junit.Assert;
import org.junit.Test;

import weka.attributeSelection.ASEvaluation;
import weka.attributeSelection.ASSearch;
import weka.attributeSelection.InfoGainAttributeEval;
import weka.attributeSelection.Ranker;
import weka.attributeSelection.ReliefFAttributeEval;
import weka.attributeSelection.SymmetricalUncertAttributeEval;

/**
 * Test Suite: Test the integrity and consistency of the values generated by
 * WekaAttributeSelection function
 * 
 * 
 * @author Irwan Krisna
 */
public class TestWekaAttributeSelection {

	/**
	 * testComparisonReal method: test the correctness and consistency generated
	 * by BridgetoWekaAttributeSelection function {@jmlSource}
	 * 
	 * @author Irwan Krisna
	 */
	@Test
	public void testComparisonReal() {
		try {
			System.out.println("=====================================");
			System.out
					.println("Test the result of various AS Evaluation algorithms -- Iris data set");
			Dataset data = FileHandler.loadDataset(new File(
					"devtools/data/iris.data"), 4, ",");
			Dataset data2 = FileHandler.loadDataset(new File(
					"devtools/data/iris.data"), 4, ",");

			/* Create various AS Evaluation Objects */
			ASEvaluation CFS = new ReliefFAttributeEval();
			ASEvaluation RFE = new ReliefFAttributeEval();
			ASEvaluation SUE = new SymmetricalUncertAttributeEval();
			ASEvaluation IGAE = new InfoGainAttributeEval();

			/* Create the AS Search Object */
			ASSearch RANKERZ = new Ranker();

			/* Wrap in bridge */
			WekaAttributeSelection wekaattrsel1a = new WekaAttributeSelection(
					CFS, RANKERZ);
			WekaAttributeSelection wekaattrsel2a = new WekaAttributeSelection(
					RFE, RANKERZ);
			WekaAttributeSelection wekaattrsel3a = new WekaAttributeSelection(
					SUE, RANKERZ);
			WekaAttributeSelection wekaattrsel4a = new WekaAttributeSelection(
					IGAE, RANKERZ);

			wekaattrsel1a.build(data);
			wekaattrsel2a.build(data);
			wekaattrsel3a.build(data);
			wekaattrsel4a.build(data);
			System.out.println("Total number of attributes: "
					+ wekaattrsel1a.num_attr);
			System.out.println("Total number of attributes: "
					+ wekaattrsel2a.num_attr);
			System.out.println("Total number of attributes: "
					+ wekaattrsel3a.num_attr);
			System.out.println("Total number of attributes: "
					+ wekaattrsel4a.num_attr);
			System.out.println("CFS\tRFE\tSUE\tIGAE\t");

			System.out.println(wekaattrsel1a.score(0) + "\t"
					+ wekaattrsel2a.score(0) + "\t" + wekaattrsel3a.score(0)
					+ "\t" + wekaattrsel4a.score(0));
			System.out.println(wekaattrsel1a.score(1) + "\t"
					+ wekaattrsel2a.score(1) + "\t" + wekaattrsel3a.score(1)
					+ "\t" + wekaattrsel4a.score(1));
			System.out.println(wekaattrsel1a.score(2) + "\t"
					+ wekaattrsel2a.score(2) + "\t" + wekaattrsel3a.score(2)
					+ "\t" + wekaattrsel4a.score(2));
			System.out.println(wekaattrsel1a.score(3) + "\t"
					+ wekaattrsel2a.score(3) + "\t" + wekaattrsel3a.score(3)
					+ "\t" + wekaattrsel4a.score(3));

			WekaAttributeSelection wekaattrsel1b = new WekaAttributeSelection(
					CFS, RANKERZ);
			WekaAttributeSelection wekaattrsel2b = new WekaAttributeSelection(
					RFE, RANKERZ);
			WekaAttributeSelection wekaattrsel3b = new WekaAttributeSelection(
					SUE, RANKERZ);
			WekaAttributeSelection wekaattrsel4b = new WekaAttributeSelection(
					IGAE, RANKERZ);

			wekaattrsel1b.build(data2);
			wekaattrsel2b.build(data2);
			wekaattrsel3b.build(data2);
			wekaattrsel4b.build(data2);
			System.out.println("Total number of attributes: "
					+ wekaattrsel1b.num_attr);
			System.out.println("Total number of attributes: "
					+ wekaattrsel2b.num_attr);
			System.out.println("Total number of attributes: "
					+ wekaattrsel3b.num_attr);
			System.out.println("Total number of attributes: "
					+ wekaattrsel4b.num_attr);
			System.out.println("CFS\tRFE\tSUE\tIGAE\t");

			System.out.println(wekaattrsel1b.score(0) + "\t"
					+ wekaattrsel2b.score(0) + "\t" + wekaattrsel3b.score(0)
					+ "\t" + wekaattrsel4b.score(0));
			System.out.println(wekaattrsel1b.score(1) + "\t"
					+ wekaattrsel2b.score(1) + "\t" + wekaattrsel3b.score(1)
					+ "\t" + wekaattrsel4b.score(1));
			System.out.println(wekaattrsel1b.score(2) + "\t"
					+ wekaattrsel2b.score(2) + "\t" + wekaattrsel3b.score(2)
					+ "\t" + wekaattrsel4b.score(2));
			System.out.println(wekaattrsel1b.score(3) + "\t"
					+ wekaattrsel2b.score(3) + "\t" + wekaattrsel3b.score(3)
					+ "\t" + wekaattrsel4b.score(3));

			/* Test the correctness of Ranking */

			Assert.assertEquals("Error    :   ", wekaattrsel1a.score(0),
					wekaattrsel1b.score(0), 0.00000000000);
			Assert.assertEquals("Error    :   ", wekaattrsel2a.score(0),
					wekaattrsel2b.score(0), 0.00000000000);
			Assert.assertEquals("Error    :   ", wekaattrsel3a.score(0),
					wekaattrsel3b.score(0), 0.00000000000);
			Assert.assertEquals("Error    :   ", wekaattrsel4a.score(0),
					wekaattrsel4b.score(0), 0.00000000000);

		} catch (IOException e) {
			e.printStackTrace();
			Assert.fail();

		}
	}

	/**
	 * testValueofRankingScore method: test the consistency of rank values
	 * generated by BridgetoWekaAttributeSelection function {@jmlSource
	 * 
	 * }
	 * 
	 * @author Irwan Krisna
	 */
	@Test
	public void testValueofRankingScore() {
		try {
			System.out.println("=====================================");
			System.out
					.println("Test the Ranking and Score Mapping values  -- Iris data set and contact lenses");
			Dataset data = FileHandler.loadDataset(new File(
					"devtools/data/iris.data"), 4, ",");
			Dataset data2 = FileHandler.loadDataset(new File(
					"devtools/data/contact-lenses.data"), 4, ",");

			/* Create various AS Evaluation Objects */
			ASEvaluation CFS = new ReliefFAttributeEval();
			ASEvaluation RFE = new ReliefFAttributeEval();

			/* Create the AS Search Object */
			ASSearch RANKERZ = new Ranker();

			/* Create the WekaAttribute Objects Object */
			WekaAttributeSelection wekaattrsel1a = new WekaAttributeSelection(
					CFS, RANKERZ);
			WekaAttributeSelection wekaattrsel2a = new WekaAttributeSelection(
					RFE, RANKERZ);

			/* Wrap in the bridge */
			wekaattrsel1a.build(data);
			wekaattrsel2a.build(data2);

			/* check the values of ranking number mapping */
			/* Test the correctness */
			Assert.assertEquals("Error    ", wekaattrsel1a.num_attr - 1,
					wekaattrsel1a.numbers.values().size());
			Assert.assertEquals("Error    ", wekaattrsel2a.num_attr - 1,
					wekaattrsel1a.numbers.values().size());

			/* Test the Scores */

			System.out.println(wekaattrsel1a.score(0));
			System.out.println(wekaattrsel2a.score(1));

		} catch (IOException e) {
			e.printStackTrace();
			Assert.fail();
		}
	}

}
